﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
namespace DynamicExecuter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            try
            {
                if (!File.Exists(this.txtFilePath.Text))
                {
                    MessageBox.Show("指定的DLL文件不存在", "文件错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    Assembly assembly = System.Reflection.Assembly.LoadFile(this.txtFilePath.Text);
                    Type type = assembly.GetType(this.txtAssembly.Text);
                    if (type != null)
                    {
                        MethodInfo method = type.GetMethod(this.txtMethod.Text);
                        object instance = null;
                        if (this.cbxDefaultConstruct.Checked)
                        {
                            instance = assembly.CreateInstance(this.txtAssembly.Text, true);
                        }

                        object[] parameters = null;
                        List<object> list = new List<object>();
                        if (!string.IsNullOrEmpty(this.txtPara.Text))
                        {
                            string[] paras = this.txtPara.Text.Replace("{", "").Replace("}", "").Replace("\"", "").Replace("'", "").Split(',');
                            string[] para = null;
                            foreach (string str in paras)
                            {
                                para = str.Split(':');
                                switch (para[0].ToLower())
                                {
                                    case "string":
                                        list.Add(para[1]);
                                        break;
                                    case "int":
                                        list.Add(Convert.ToInt32(para[1]));
                                        break;
                                    case "double":
                                        list.Add(Convert.ToDouble(para[1]));
                                        break;
                                    case "datetime":
                                    case "date":
                                        list.Add(Convert.ToDateTime(para[1]));
                                        break;
                                }
                            }
                            parameters = list.ToArray();
                        }


                        object res = method.Invoke(instance, parameters);
                        if (res != null)
                        {
                            MessageBox.Show(res.ToString());
                        }
                        MessageBox.Show("方法成功调用");

                    }
                    else
                    {
                        MessageBox.Show("指定的方法名不存在，请确认后重新输入");
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

    }
}
